/*
 * TODO language 
 * Copyright (c) 2010 TODO Team
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef TODO_OPCODES_H
#define TODO_OPCODES_H
#include "internal/config.h"
#include "typeman/types.h"
#include "compiler/vm.h"

namespace todo { namespace opcode {
	
enum OpcodeType {
	TODO_PRINT,
	TODO_PRINTLN,
	TODO_METHOD_CALL,
	TODO_STATIC_METHOD_CALL,
	TODO_OFFSET,
	TODO_SLICE,
	TODO_ASSIGN,
	TODO_DECLARE_VAR,
	TODO_PRE_INC,
	TODO_PRE_DEC,
	TODO_ASSIGN_MINUS,
	TODO_MINUS,
	TODO_ASSIGN_PLUS,
	TODO_PLUS,
	TODO_ASSIGN_DIV,
	TODO_DIV,
	TODO_ASSIGN_MULT,
	TODO_MULT,
	TODO_ASSIGN_MOD,
	TODO_MOD,
	TODO_JMPZ,
	TODO_JMP,
	TODO_IS_EQUAL,
	TODO_IS_NOT_EQUAL,
	TODO_POS_INC,
	TODO_POS_DEC,
	TODO_SUBSCRIPT,
	TODO_GREATER,
	TODO_LESS,
	TODO_GREATER_EQUAL,
	TODO_LESS_EQUAL,
	TODO_VECTOR,
	TODO_VECTOR_ADD,
	TODO_MAP,
	TODO_MAP_ADD,
	TODO_DECLARE_FUNC,
	TODO_FUNC_CALL,
	TODO_END_FUNC,
	TODO_RETURN,
	TODO_EXIT,
	TODO_XOR,
	TODO_ASSIGN_XOR,
	TODO_SL,
	TODO_ASSIGN_SL,
	TODO_SR,
	TODO_ASSIGN_SR,
	TODO_BW_OR,
	TODO_ASSIGN_BW_OR,
	TODO_BW_AND,
	TODO_ASSIGN_BW_AND,
	TODO_BREAK,
	TODO_JMPZ_EX
} ATT_PACKED;

class Opcode {
public:	
	unsigned int op_num;
	OpcodeType type;
	todo::Data op1, op2;
	void* info;
	todo::vm::Engine::vm_handler handler;

	Opcode() : info(NULL) { }
	Opcode(OpcodeType type_) : type(type_), info(NULL) { }
	~Opcode();

	void set_type(OpcodeType type_) { type = type_; }
	
	void set_op1(todo::Data& val) {
		op1 = val;

		if (op1.data_type == todo::TODO_DYNAMIC) {
			op1.result_flag = todo::REFERENCED;
		}
	}
	
	void set_op1_(todo::Data& val) { op1 = val; }
	
	void set_op2(todo::Data& val) { op2 = val; }
	
	void set_handler(todo::vm::Engine::vm_handler handler_) { handler = handler_; }
	
	void dump(void) const;
};

}} // todo::opcode

#endif // TODO_OPCODES_H
